﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PicSolve
{
    public class PLineBuilder
    {
        public static PLine FilledOnLeft(int nbBox, int lineLength)
        {
            if (nbBox > lineLength)
            {
                throw new InvalidOperationException("nbBox > lineLength");
            }
            return WithSimplePatternInside(0, new int[1] { nbBox }, lineLength);
        }

        public static PLine FilledOnRight(int nbBox, int lineLength)
        {
            if (nbBox > lineLength)
            {
                throw new InvalidOperationException("nbBox > lineLength");
            }
            return WithSimplePatternInside(lineLength - nbBox, new int[1] { nbBox }, lineLength);
        }

        public static PLine FilledInside(int decal, int nbBox, int lineLength)
        {
            if (decal + nbBox > lineLength)
            {
                throw new InvalidOperationException("decal + nbBox > lineLength");
            }
            
            return WithSimplePatternInside(decal, new int[1] { nbBox }, lineLength);
        }

        public static PLine WithSimplePatternInside(int decal, int[] indices, int lineLength)
        {
            if (indices.Sum(i => i) + (indices.Length - 1) + decal > lineLength)
            {
                throw new InvalidOperationException("Impossible de créer la ligne, la taille totale des indices et du décalage est supérieure à la taille de la ligne");
            }

            PLine ret = new PLine(lineLength);
            int idxLine = decal;
            for (int i = 0; i < indices.Length; i++)
            {
                for (int j = 0; j < indices[i]; j++)
                {
                    ret[idxLine] = PValue.Box;
                    idxLine++;
                }
                if (i < indices.Length - 1)
                {
                    ret[idxLine] = PValue.NoBox;
                    idxLine++;
                }
            }
            return ret;
        }

        public static PLine FilledWithSimplePattern(int[] indices, int lineLength)
        {
            if (indices.Sum(i => i) + indices.Length - 1 != lineLength)
            {
                throw new InvalidOperationException("Impossible de créer la ligne, la taille totale des indices ne correspond pas à la taille de la ligne");
            }

            return WithSimplePatternInside(0, indices, lineLength);
        }
    }
}
